package cn.sumpu.app.guoguojie.util;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import weibo4j.User;
import weibo4j.Weibo;
import weibo4j.WeiboException;
import weibo4j.http.AccessToken;
import weibo4j.http.ImageItem;
import weibo4j.http.RequestToken;

public class SinaWeibo {
	private static RequestToken requestToken = null;

	public static String openOauth(String weiboSinaAppCallBack, String oauthType)
			throws IOException, WeiboException {
		Weibo weibo = new Weibo(); 
		weiboSinaAppCallBack = weiboSinaAppCallBack + "?oauthType=" + oauthType;
		weibo.setOAuthConsumer(Common.WEIBO_SINA_APP_KEY,
				Common.WEIBO_SINA_APP_SECRET);
		requestToken = weibo.getOAuthRequestToken(weiboSinaAppCallBack);

		return requestToken.getAuthorizationURL();

	}

	public static Map<String, String> oauth(String oauthVerifier,
			String oauthType) throws WeiboException {
		Map<String, String> map = new HashMap<String, String>();
		AccessToken accessToken = null;
		try {
			accessToken = requestToken.getAccessToken(oauthVerifier);
			Long lo = accessToken.getUserId();
			Weibo weibo = new Weibo();
			weibo.setOAuthConsumer(Common.WEIBO_SINA_APP_KEY,
					Common.WEIBO_SINA_APP_SECRET);
			weibo.setToken(accessToken.getToken(), accessToken.getTokenSecret());
			User user = weibo.showUser(lo.toString());
			map.put("accessToken", accessToken.getToken());
			map.put("accessSecret", accessToken.getTokenSecret());
			map.put("outAccountName", user.getName());
			map.put("outAccountId", lo.toString());
			// 关注果果街新浪微博
			//weibo.destroyFriendship(Common.WEIBO_SINA_GUOGUOJIE_UID);
			Boolean isFriend=weibo.existsFriendship(lo.toString(), Common.WEIBO_SINA_GUOGUOJIE_UID);
			if(!isFriend){
				weibo.createFriendshipByUserid(Common.WEIBO_SINA_GUOGUOJIE_UID);
			}

		} catch (WeiboException e) {
			e.printStackTrace();
		}
		return map;
	}

	// 发布文字微博
	public static void sendMessage2Sina(String text, String accessToken,
			String accessSecret) {

		Weibo weibo = new Weibo();
		weibo.setOAuthConsumer(Common.WEIBO_SINA_APP_KEY,
				Common.WEIBO_SINA_APP_SECRET);
		weibo.setToken(accessToken, accessSecret);
		try {
			weibo.updateStatus(text);
		} catch (WeiboException e) {
			e.printStackTrace();
		}
	}

	// 发布带图片的微博(网络图片路径)
	public static void sendPic2Sina(String text, String picUrl,
			String accessToken, String accessSecret) throws IOException {
		try {
			Weibo weibo = new Weibo();
			weibo.setOAuthConsumer(Common.WEIBO_SINA_APP_KEY,
					Common.WEIBO_SINA_APP_SECRET);
			weibo.setToken(accessToken, accessSecret);
			byte[] content = Common.picUrlToByte(picUrl);
			ImageItem pic = new ImageItem("pic", content);
			weibo.uploadStatus(text, pic);
		} catch (WeiboException e) {
			e.printStackTrace();
		}
	}

	// 发布带图片的微博(本地图片路径)
	public static void sendLocalPicSina(String text, String picUrl,
			String accessToken, String accessSecret) throws IOException {
		ImageUtil.makeWatermark(text, picUrl, new java.awt.Font(java.awt.Font.MONOSPACED, java.awt.Font.BOLD, 30),Color.RED, 4);
		try {
			Weibo weibo = new Weibo();
			weibo.setOAuthConsumer(Common.WEIBO_SINA_APP_KEY,
					Common.WEIBO_SINA_APP_SECRET);
			weibo.setToken(accessToken, accessSecret);
			File file = new File(picUrl);
			weibo.uploadStatus(text, file);
		} catch (WeiboException e) {
			e.printStackTrace();
		}
	}

	// 取消关注
	public static void cancelAttentionSinaWeibo(String accessToken,
			String accessSecret) throws WeiboException {
		Weibo weibo = new Weibo();
		weibo.setOAuthConsumer(Common.WEIBO_SINA_APP_KEY,
				Common.WEIBO_SINA_APP_SECRET);
		weibo.setToken(accessToken, accessSecret);
		weibo.destroyFriendship(Common.WEIBO_SINA_GUOGUOJIE_UID);
	}

	// 获取用户的粉丝
	public static List<User> getUserFans(String accessToken, String accessSecret)
			throws IOException {
		try {
			Weibo weibo = new Weibo();
			weibo.setOAuthConsumer(Common.WEIBO_SINA_APP_KEY,
					Common.WEIBO_SINA_APP_SECRET);
			weibo.setToken(accessToken, accessSecret);
			List<User> list = weibo.getFollowersStatuses();
			return list;
		} catch (WeiboException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 获取用户关注的人
	public static List<User> getUserFollows(String accessToken,
			String accessSecret) throws IOException {
		try {
			Weibo weibo = new Weibo();
			weibo.setOAuthConsumer(Common.WEIBO_SINA_APP_KEY,
					Common.WEIBO_SINA_APP_SECRET);
			weibo.setToken(accessToken, accessSecret);
			List<User> list = weibo.getFriendsStatuses();
			return list;
		} catch (WeiboException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 获取互相关注的用户
	public static List<User> getUserBothFollows(String userId,
			String accessToken, String accessSecret) throws IOException {
		try {
			Weibo weibo = new Weibo();
			weibo.setOAuthConsumer(Common.WEIBO_SINA_APP_KEY,
					Common.WEIBO_SINA_APP_SECRET);
			weibo.setToken(accessToken, accessSecret);
			List<User> myFans = weibo.getFollowersStatuses();
			List<User> myFollow = weibo.getFriendsStatuses();
			
			Set<User> set=new HashSet<User>();
			for (User u : myFans) {
				Long uid = u.getId();
				boolean bool = weibo.existsFriendship(userId, uid.toString());
				if (bool) {
					set.add(u);
				}
			}
			for (User u : myFollow) {
				Long uid = u.getId();
				boolean bool = weibo.existsFriendship(uid.toString(), userId);
				if (bool) {
					set.add(u);
				}
			}
			List<User> list = new ArrayList<User>(set);
			return list;
		} catch (WeiboException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) throws IOException {
		List<User> list = getUserBothFollows("2389518775","864fef527eaf025fd47110e35f32d35b","9c62d5d7373fa378b8de44a8c8df6b39");
		System.out.println(list.size());

	}

}
